import { formatDate, formatTime } from '@/utils/jk/jk-tool';
import { workforceManagementShiftGetByTime } from '@/api/schedule/shift';
import { workforcemanagementScheduleList } from '../../../../api/schedule/scheduling';
export default {
    data() {
        return {
            shiftList: [
                {
                    id: 1,
                    name: '白班',
                    beginHour: '08:00:00',
                    endHour: '20:00:00'
                },
                {
                    id: 2,
                    name: '晚班',
                    beginHour: '20:00:00',
                    endHour: '08:00:00'
                }
            ],
            tableColumnList: [],
            machineFaceList: [{ id: 'A' }, { id: 'B' }],
            spinPositionList: [],
            isCustomDate: false,
            dateType: 0,
            dateTypeList: [
                // { id: 1, name: '当班' },
                // { id: 2, name: '上一班' },
                { id: 0, name: '今天' },
                { id: -1, name: '昨天' },
                { id: -2, name: '前天' }
            ],
            currentAndLastShiftInfo: {
                currentShiftInfo: {
                    belongDate: ''
                },
                lastShiftInfo: {
                    belongDate: ''
                }
            }
        };
    },
    computed: {
        setTitleStyle() {
            return {
                fontWeight: 500,
                fontSize: '16px',
                borderLeft: 'solid 4px #2d8cf0',
                paddingLeft: '10px',
                color: '#515a6e'
            };
        }
    },
    methods: {
        // 定义一个方法来处理日期和时间
        formatBelongDates(dates, shiftCode, pickerTime) {
            return dates.map((date, index) => {
                const defaultTime = index === 0 ? '00:00:00' : '23:59:59';
                const time =
                this.pickerTime && pickerTime.length > 0 ? pickerTime[index] : defaultTime;
                // 如果是shiftCode为2且是第二个日期，增加一天
                if (shiftCode === 2 && index === 1) {
                    const dateObj = new Date(date); // 将日期字符串转为Date对象
                    dateObj.setDate(dateObj.getDate() + 1); // 增加一天
                    date = `${dateObj.getFullYear()}-${(dateObj.getMonth() + 1).toString().padStart(2, '0')}-${dateObj.getDate().toString().padStart(2, '0')}`;
                }
                return `${date} ${time}`;
            });
        },
        getCurrentShiftInfoRequest() {
            const belongTime = formatTime(Date.now());
            return workforceManagementShiftGetByTime({
                workshopCode: 'HF',
                // workshopId: '32',
                belongTime
            }).then((res) => {
                if (res.data.status === 200) {
                    const resData = res.data.res || {};
                    const { name } = resData.currentShiftInfo || {};
                    // 通过名称去匹配
                    const filterId = this.shiftList.find(
                        (i) => i.name === name
                    )?.id;
                    // this.$set(this.searchParams, 'shiftCode', filterId);
                    this.$set(this.searchParams, 'fromShiftCode', filterId);
                    this.$set(this.searchParams, 'toShiftCode', filterId);
                }
            });
        },
        onIsCustomDateChange(e) {
            console.log(e);
            if (!e) {
                this.dateType = 0;
                const date = formatDate(Date.now());
                this.$set(this.searchParams, 'fromDate', date);
                this.$set(this.searchParams, 'toDate', date);
                this.$set(this.searchParams, 'toShiftCode', undefined);
            } else {
                this.dateType = undefined;
            }
        },
        onHandleShift(e) {
            this.$set(this.searchParams, 'toShiftCode', e);
        },
        async onDateTypeChange(e) {
            let date = '';
            // 当班, 注：不能取当天，因为班次有可能跨天
            if (e === 1) {
                await this.getScheduleShiftForTimeRequest();
                date = this.currentAndLastShiftInfo.currentShiftInfo.belongDate;
                this.$set(
                    this.searchParams,
                    'shiftId',
                    this.currentAndLastShiftInfo.currentShiftInfo.id
                );
            } else if (e === 2) {
                await this.getScheduleShiftForTimeRequest();
                date = this.currentAndLastShiftInfo.lastShiftInfo.belongDate;
                this.$set(
                    this.searchParams,
                    'shiftId',
                    this.currentAndLastShiftInfo.lastShiftInfo.id
                );
            } else {
                date = formatDate(Date.now() + e * 24 * 60 * 60 * 1000);
                this.$set(this.searchParams, 'shiftId', null);
            }
            // this.$set(this.searchParams, 'belongDates', [date, date]);
            this.$set(this.searchParams, 'fromDate', date);
            this.$set(this.searchParams, 'toDate', date);
        },
        getShiftListByWorkshopRequest() {
            /* return workforcemanagementShiftPage({
				auditState: 1,
				workshopId: this.searchParams.workshopId
			}).then(res => {
				if (res.data.status === 200) {
					this.shiftList = res.data.res || [];
				}
			});*/
        },
        // getDateScheduleShiftOptionRequest() {
        //     if (this.searchParams.workshopId && this.searchParams.belongDates) {
        //         return workforcemanagementScheduleList({
        //             belongDate: this.searchParams.belongDates,
        //             workshopId: this.searchParams.workshopId
        //         }).then((res) => {
        //             if (res.data.status === 200) {
        //                 const resData = res.data.res;
        //                 if (resData && resData.length) {
        //                     this.scheduleShiftList =
        //                         resData[0].scheduleShiftList;
        //                     this.$set(
        //                         this.searchParams,
        //                         'scheduleShiftId',
        //                         this.scheduleShiftList &&
        //                             this.scheduleShiftList.length
        //                             ? this.scheduleShiftList[0].id
        //                             : null
        //                     );
        //                 }
        //             }
        //         });
        //     }
        // },
        getScheduleShiftForTimeRequest() {
            if (this.searchParams.workshopId) {
                return workforceManagementShiftGetByTime({
                    workshopId: this.searchParams.workshopId,
                    belongTime: formatTime(Date.now())
                }).then((res) => {
                    if (res.data.status === 200) {
                        const resData = res.data.res;
                        this.currentAndLastShiftInfo = resData || {};
                    }
                });
            }
        },
        getIngotRange(data) {
            const params = JSON.parse(JSON.stringify(data));
            if (params.startIngotNo) { params.startIngotNo = Number(params.startIngotNo); }
            if (params.endIngotNo) { params.endIngotNo = Number(params.endIngotNo); }
            /* if (params.startIngotNo && params.endIngotNo) {
				if (params.startIngotNo > params.endIngotNo) {
					tipsBar(this, 'custom', 'warning', '结束锭号不能小于开始锭号!');
					return;
				}
			}*/
            return params;
        },
        onIngotNumberChange(e, key) {
            if (key === 'startIngotNo' && !this.searchParams.endIngotNo) {
                this.$set(
                    this.searchParams,
                    'endIngotNo',
                    e && !isNaN(Number(e)) ? Number(e) : ''
                );
            }
            this.$set(
                this.searchParams,
                key,
                e && !isNaN(Number(e)) ? Number(e) : ''
            );
        },
        async getDependentData() {
            // this.$set(this.searchParams, 'belongDates', [
            //     formatDate(Date.now()),
            //     formatDate(Date.now())
            // ]);
            // this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
            // this.workshopList = this.$store.state.user.userWorkshop.workshopList;
            const date = formatDate(Date.now());
            this.$set(this.searchParams, 'fromDate', date);
            this.$set(this.searchParams, 'toDate', date);
            this.tableLoading = true;
            await this.getMenuColumnListRequest();
            // await this.getDateScheduleShiftOptionRequest();
            await this.getShiftListByWorkshopRequest();
            // await this.getScheduleShiftForTimeRequest();
            await this.getListRequest();
            this.tableLoading = false;
        },
        /** 搜索按钮操作 */
        onSearchBtn() {
            this.searchParams.pageNo = 1;
            this.totalCount = 1;
            this.getListRequest();
        }
    }
};
